package com.intretech.umsin.oss.service.impl;


import com.intretech.umsin.oss.config.FtpClientProperties;
import com.intretech.umsin.oss.entity.FileType;
import com.intretech.umsin.oss.service.MetadataStoreService;
import com.intretech.umsin.oss.util.ftppool.FtpClientTemplate;
import com.intretech.umsin.oss.vo.FileInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @version V1.0
 * @ClassName: FtpOssServiceImpl
 * @Description: TODO(FTP实现的OSS)
 * @author: 李启联
 * @date: 2022-04-25 14:13
 * @Copyright: Copyright (c), 2020
 */
@Service
@Slf4j
public class FtpOssServiceImpl extends AbstractFileService {

    @Autowired
    private FtpClientProperties ftpClientProperties;

    @Autowired(required = false)
    private FtpClientTemplate ftpClientTemplate;

    @Autowired(required = false)
    private MetadataStoreService metadataStoreService;

    /**
     * OSS存储类型
     *
     * @return
     */
    @Override
    public FileType fileType() {
        return FileType.FTP;
    }

    /**
     * 文件元数据存储服务
     *
     * @return
     */
    @Override
    protected MetadataStoreService getMetadataStoreService() {
        return metadataStoreService;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param fileInfo
     */
    @Override
    protected void uploadFile(MultipartFile file, FileInfoVo fileInfo) throws Exception {
        String workDir = getWorkDir(ftpClientProperties.getWorkDir());
        if(fileInfo.getFilePathPrefix().length() >1){//设置文件的路径前缀
            String prefix = getWorkDir(fileInfo.getFilePathPrefix());
            fileInfo.setFilePathPrefix(workDir + prefix);
        }else{
            fileInfo.setFilePathPrefix(workDir);
        }
        ftpClientTemplate.upload(file.getInputStream(),fileInfo.getFilePathPrefix() +"/"+ fileInfo.getTagsName());

    }

    /**
     * 删除文件资源
     *
     * @param fileInfo
     * @return
     */
    @Override
    protected boolean deleteFile(FileInfoVo fileInfo) {
        try {
            ftpClientTemplate.deleteFile(fileInfo.getPath());
            return true;
        }catch (Exception e) {
            return false;
        }
    }

    /**
     * 对查询返回的文件列表设置其对应文件访问的URL
     *
     * @param fileInfo
     */
    @Override
    protected void setFileUrl(FileInfoVo fileInfo) {
        fileInfo.setUrl(ftpClientProperties.getDomain() + fileInfo.getPath());
    }

    /**
     * 下载文件
     *
     * @param fileInfo
     * @return
     */
    @Override
    public byte[] downloadFile(FileInfoVo fileInfo) throws Exception {
        return ftpClientTemplate.download(fileInfo.getPath());
    }


    private String getWorkDir(String workDir2) {
        String workDir = workDir2.replace("\\", "/");
        workDir = workDir.startsWith("/") ? workDir : "/" + workDir;
        workDir = workDir.endsWith("/") ? workDir.substring(0, workDir.length() - 1) : workDir;
        return workDir;
    }

}